<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>电影信息添加页面</title>
    <link href="../../static/jquery-easyui-1.3.3/themes/default/easyui.css" th:href="@{/static/jquery-easyui-1.3.3/themes/default/easyui.css}" rel="stylesheet" type="text/css"/>
    <link href="../../static/jquery-easyui-1.3.3/themes/icon.css" th:href="@{/static/jquery-easyui-1.3.3/themes/icon.css}" rel="stylesheet" type="text/css"/>
    <script src="../../static/jquery-easyui-1.3.3/jquery.min.js" th:src="@{/static/jquery-easyui-1.3.3/jquery.min.js}" type="text/javascript"></script>
    <script src="../../static/jquery-easyui-1.3.3/jquery.easyui.min.js" th:src="@{/static/jquery-easyui-1.3.3/jquery.easyui.min.js}" type="text/javascript"></script>
    <script src="../../static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js" th:src="@{/static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js}" type="text/javascript"></script>
    <script src="../../static/ckeditor/ckeditor.js" th:src="@{/static/ckeditor/ckeditor.js}" type="text/javascript"></script>
    <script>
        $(document).ready(function() {
            // Ensure CKEditor is loaded before replacing the textarea
            if (typeof CKEDITOR !== 'undefined') {
                CKEDITOR.replace('content');
            } else {
                console.error('CKEditor is not loaded.');
            }
        });

        // 提交表单
        function submitData() {
            // EasyUI无刷新提交
            $('#fm').form('submit', {
                url: '/admin/movie/save',
                onSubmit: function () {
                    // 获取内容输入框中的内容
                    var content = CKEDITOR.instances.content.getData();

                    // if (!content) {
                    //     $.messager.alert('系统提示', '内容输入框中不能为空！');
                    //     return false;
                    // }
                    // 验证表单字段，如果全通过则提交表单
                    return $(this).form('validate');
                },
                success: function (result) {
                    try {
                        var res = JSON.parse(result);
                        if (res.success) {
                            $.messager.alert('系统提示', '保存成功');
                            resetValue(); // 重置所有输入框的值
                        } else {
                            $.messager.alert('系统提示', '保存失败');
                        }
                    } catch (e) {
                        console.error('Error parsing JSON response:', e);
                        if (result.indexOf('<html') !== -1) {
                            $.messager.alert('系统提示', '保存失败，服务器返回了一个错误页面');
                        } else {
                            $.messager.alert('系统提示', '保存失败，服务器返回无效的响应');
                        }
                    }
                }
            });
        }

        // 重置所有输入框中的值
        function resetValue() {
            $('#name').val('');
            $('#title').val('');
            $('#imageFile').val('');
            $('#no').prop('checked', true);
            CKEDITOR.instances.content.setData('');
        }
    </script>
</head>

<body style="margin: 10px;">

<div id="p" class="easyui-panel">
    <form id="fm" method="post" enctype="multipart/form-data">
        <table cellspacing="20px">
            <tr>
                <td width="80px">电影名称：</td>
                <td>
                    <input type="text" id="name" name="name" style="width: 400px;" class="easyui-validatebox" required="required">
                </td>
            </tr>
            <tr>
                <td width="80px">标题：</td>
                <td>
                    <input type="text" id="title" name="title" style="width: 400px;" class="easyui-validatebox" required="required">
                </td>
            </tr>
            <tr>
                <td width="80px">电影图片：</td>
                <td>
                    <input type="file" id="imageFile" name="imageFile">
                </td>
            </tr>
            <tr>
                <td width="80px">设置热门：</td>
                <td>
                    <input type="radio" id="yes" value="1" name="hot">是&nbsp;
                    <input type="radio" id="no" value="0" name="hot" checked="checked">否
                </td>
            </tr>
            <tr>
                <td valign="top">内容</td>
                <td>
                    <textarea name="content" id="content" cols="80" rows="30"></textarea>
                </td>
            </tr>
            <tr>
                <td></td>
                <td>
                    <a href="javascript:submitData()" class="easyui-linkbutton" data-options="iconCls:'icon-submit'">
                        发布
                    </a>
                </td>
            </tr>
        </table>
    </form>
</div>

</body>

</html>